<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/5/18
 * Time: 19:25
 * Desc: 门店管理员
 */
namespace app\store\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Manager extends BasicAdmin{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemUser';

    /**
     * @Author  lingyun
     * @Desc    门店管理员
     * @return array|string
     */
    public function index(){
        $this->title = '门店管理员';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $map['is_deleted'] = 0;
        $map['store_id'] = $store_id;

        $db = Db::name($this->table)->where($map)->order('id desc');
        foreach (['username', 'phone', 'mail'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        if (isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $db->whereBetween('login_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        return parent::_list($db);
    }

    /**
     * 用户添加
     */
    public function add()
    {
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $data_arr = [
                'realname'=>$post['realname'],
                'username'=>$post['username'],
                'phone'=>$post['phone'],
                'store_id'=>$post['store_id'],
                'desc'=>$post['desc'],
                'authorize'=>implode(',',$post['authorize']),
                'create_at'=>date('Y-m-d H:i:s',time())
            ];
            $res = db('system_user')->insert($data_arr);
            $res>0?$this->success('保存成功',''):$this->error('保存失败','');
        }else{
            $id = input('id');
            $vo = db('system_user')->where('id',$id)->find();
            if(!empty($vo['authorize'])){
                $vo['authorize'] = explode(',',$vo['authorize']);
            }else{
                $vo['authorize'] = array();
            }
            $store_id = session('user')['store_id'];
            $role = db('system_auth')->where(['status'=>1,'id'=>['not in','1,2']])->select();

            $this->assign('store_id',$store_id);
            $this->assign('authorizes',$role);
            $this->assign('vo',$vo);
            return $this->fetch('form');
        }
    }

    /**
     * 用户编辑
     */
    public function edit()
    {
        $request = request();
        if($request->isPost()){
            $post = input('post.');
            $data_arr = [
                'realname'=>$post['realname'],
                'phone'=>$post['phone'],
                'store_id'=>$post['store_id'],
                'desc'=>$post['desc'],
                'authorize'=>implode(',',$post['authorize']),
                'create_at'=>date('Y-m-d H:i:s',time())
            ];
            $res = db('system_user')->where('id',$post['id'])->update($data_arr);
            $res!==false?$this->success('保存成功',''):$this->error('保存失败','');
        }
        $id = input('id');
        $vo = db('system_user')->where('id',$id)->find();
        if(!empty($vo['authorize'])){
            $vo['authorize'] = explode(',',$vo['authorize']);
        }else{
            $vo['authorize'] = array();
        }
        $store_id = session('user')['store_id'];
        $role = db('system_auth')->where(['status'=>1,'id'=>['not in','1,2']])->select();
//        $user = db('system_user')->where('id',input('id'))->find();
//        $user['authorize'] = explode(',',$user['authorize']);
        $this->assign('authorizes',$role);
        $this->assign('store_id',$store_id);
        $this->assign('vo',$vo);
        return $this->fetch('form');
    }

    /**
     * 用户密码修改
     */
    public function pass()
    {
        if ($this->request->isGet()) {
            $this->assign('verify', false);
            return $this->_form($this->table, 'pass');
        }
        $post = $this->request->post();
        if ($post['password'] !== $post['repassword']) {
            $this->error('两次输入的密码不一致！');
        }
        $data = ['id' => $post['id'], 'password' => md5($post['password'])];
        if (DataService::save($this->table, $data, 'id')) {
            $this->success('密码修改成功，下次请使用新密码登录！', '');
        }
        $this->error('密码修改失败，请稍候再试！');
    }

    /**
     * 删除用户
     */
    public function del()
    {
        if (in_array('10000', explode(',', $this->request->post('id')))) {
            $this->error('系统超级账号禁止删除！');
        }
        if (DataService::update($this->table)) {
            $this->success("用户删除成功！", '');
        }
        $this->error("用户删除失败，请稍候再试！");
    }

    /**
     * 用户禁用
     */
    public function forbid()
    {
        if (in_array('10000', explode(',', $this->request->post('id')))) {
            $this->error('系统超级账号禁止操作！');
        }
        if (DataService::update($this->table)) {
            $this->success("用户禁用成功！", '');
        }
        $this->error("用户禁用失败，请稍候再试！");
    }

    /**
     * 用户禁用
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("用户启用成功！", '');
        }
        $this->error("用户启用失败，请稍候再试！");
    }


}